package tq.com.tqteacher.discuss.vh;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQCommentOverInfoModel;

public class TQRightViewHold extends TQDiscussViewHold {
    public static TQRightViewHold instance(Context pContext, ViewGroup pViewGroup){
        View lView = LayoutInflater.from(pContext).inflate(R.layout.vh_discuss_right, pViewGroup, false);
        return new TQRightViewHold(lView);
    }

    private TextView userNameView;
    private ImageView userIconView;

    private TextView txtContentView;

    private FrameLayout mFrameLayout;
    private ImageView imgContentView;
    private ImageView playButton;

    public TQRightViewHold(View pView){
        super(pView);

        userIconView = pView.findViewById(R.id.header_icon);
        userNameView = pView.findViewById(R.id.name);

        txtContentView = pView.findViewById(R.id.text_content);

        mFrameLayout = pView.findViewById(R.id.img);
        imgContentView = pView.findViewById(R.id.img_content);
        playButton = pView.findViewById(R.id.icon_bf);

        imgContentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mIDiscussItemClickListener != null){
                    mIDiscussItemClickListener.openPic(mCommentInfoModel);
                }
            }
        });
        imgContentView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                if(!canDelete()) return;
            }
        });
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mIDiscussItemClickListener != null){
                    mIDiscussItemClickListener.openVideo(mCommentInfoModel);
                }
            }
        });
    }

    public void bindData(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel){
        mCommentInfoModel = pInfoModel;
        if(mCommentInfoModel == null) return;

        userNameView.setText(pInfoModel.getUserInfo().getName());
        RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.tx1).error(R.drawable.tx1)
                .circleCrop();

        String url = TQPictureUpload.downloadPath(pInfoModel.getUserInfo().getPic());
        Glide.with(itemView).load(url).apply(lOptions).into(userIconView);

        int d = (int)itemView.getContext().getResources().getDisplayMetrics().density;

        if(pInfoModel.getType() == TQSystemDefine.LeaveMsgType_image){
            playButton.setVisibility(View.GONE);
            txtContentView.setVisibility(View.GONE);
            mFrameLayout.setVisibility(View.VISIBLE);
            imgContentView.setVisibility(View.VISIBLE);

            RequestOptions lOptions1 = new RequestOptions().placeholder(R.drawable.default_pic)
                    .error(R.drawable.default_pic).override(Target.SIZE_ORIGINAL, 300 * d);
            String imgUrl = TQPictureUpload.downloadPath(mCommentInfoModel.getContent());
            Glide.with(itemView).load(imgUrl).apply(lOptions1).into(imgContentView);
        }else if(pInfoModel.getType() == TQSystemDefine.LeaveMsgType_video){
            txtContentView.setVisibility(View.GONE);
            mFrameLayout.setVisibility(View.VISIBLE);
            imgContentView.setVisibility(View.VISIBLE);
            playButton.setVisibility(View.VISIBLE);

            RequestOptions lOptions1 = new RequestOptions().placeholder(R.drawable.default_pic)
                    .error(R.drawable.default_pic);
            String imgUrl = TQPictureUpload.downloadPath(mCommentInfoModel.getContent());
            Glide.with(itemView).load(imgUrl).apply(lOptions1).into(imgContentView);

        }else{
            txtContentView.setVisibility(View.VISIBLE);
            mFrameLayout.setVisibility(View.GONE);
            txtContentView.setText(mCommentInfoModel.getContent());
        }

    }
}
